import {doFetch, RETURN_TYPE, SUBMIT_TYPE} from "./Fetch";

const cfg = {
    requestParamsProcessor: undefined,//请求参数处理器
};

export function registerRequestParamsProcessor(processor) {
    if (typeof processor === 'function') {
        cfg.requestParamsProcessor = processor;
    }
}

/**
 * {
        url = "",
        method = "GET",
        timeout = 0,
        urlParams,
        bodyParams,
        headers,
        submitType = SUBMIT_TYPE.URL_ENCODED,
        returnType = RETURN_TYPE.TEXT
    }
 * @param params
 */
export function requestText(params) {
    params = {
        timeout: 20000,
        ...params,
        method: params.method || "GET",
        returnType: RETURN_TYPE.TEXT,
        submitType: params.submitType || SUBMIT_TYPE.URL_ENCODED
    };
    return request(params);
}

export function requestJson(params) {
    params = {
        timeout: 20000,
        ...params,
        method: params.method || "GET",
        returnType: RETURN_TYPE.JSON,
        submitType: params.submitType || SUBMIT_TYPE.URL_ENCODED
    };
    return request(params);
}

export function submitFormData(params) {
    params = {
        timeout: 20000,
        ...params,
        method: "POST",
        returnType: RETURN_TYPE.JSON,
        submitType: SUBMIT_TYPE.URL_ENCODED
    };
    return request(params);
}

export function submitJsonData(params) {
    params = {
        timeout: 20000,
        ...params,
        method: "POST",
        returnType: RETURN_TYPE.JSON,
        submitType: SUBMIT_TYPE.JSON
    };
    return request(params);
}

export function submitMultipartFormData(params) {
    let {bodyParams} = params;
    let formData = new FormData();
    Object.keys(bodyParams).forEach((name) => {
        formData.append(name, bodyParams[name]);
    });
    params.bodyParams = formData;
    params = {
        ...params,
        timeout: 0,
        method: "POST",
        returnType: RETURN_TYPE.JSON,
        submitType: SUBMIT_TYPE.FORM_DATA
    };
    return request(params);
}

export function request(params) {
    let {
        url,
        urlParams,
        bodyParams,
        headers,
    } = params;
    if (cfg.requestParamsProcessor) {
        let options = cfg.requestParamsProcessor({url, bodyParams, urlParams, headers});
        if (options) {
            params.url = options.url || params.url;
            params.bodyParams = options.bodyParams || params.bodyParams;
            params.urlParams = options.urlParams || params.urlParams;
            params.headers = options.headers || params.headers;
        }
    }
    return new Promise((resolve, reject) => {
        doFetch(params).then(
            result => {
                if (typeof result === 'object') {
                    let {code, data} = result;
                    if (typeof code !== 'undefined') {
                        if (code !== 0) {
                            reject({
                                code: code ? code : -1,
                                msg: result.message || result.msg || "服务器返回数据错误",
                                data
                            });
                            return;
                        }
                    }
                }
                resolve(result);
            },
            error =>
                reject({code: -1, msg: error.message || error.msg, error: error})
        );
    });
}